using System;
using System.Windows.Forms;
using System.Drawing;

public class DGVFileEditingControl : UserControl, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    bool valueChanged = false;
    int rowIndex = 0;
    string oldValue = null;
    private OpenFileDialog ofdFile;
    private TextBox txtFile;
    private Button btnBrowse;

    public DGVFileEditingControl()
    {
        InitializeComponent();
        //this.txtFilePath.LostFocus += new EventHandler(txtFilePath_LostFocus);
    }

    public String Value
    {
        get
        {
            return txtFile.Text;
        }
        set
        {
            txtFile.Text = value;
        }
    }

    /*void txtFilePath_LostFocus(object sender, EventArgs e)
    {
        NotifyChange();
    }*/
    
    private void btnBrowse_Click(object sender, EventArgs e)
    {
        valueChanged = false;
        
        if (ofdFile.ShowDialog() == DialogResult.OK)
            this.txtFile.Text = ofdFile.FileName;
        NotifyChange();
    }
    
    private void NotifyChange() 
    {
        if (this.txtFile.Text != oldValue)
        {
            valueChanged = true; 
            dataGridView.NotifyCurrentCellDirty(true);
        }
    }
    
    public string OldValue 
    {
        get
        {
            return oldValue;
        }
        set
        {
            oldValue = value;
        }
    }
    
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
    }
    
    public Cursor EditingControlCursor
    {
        get
        {
            return Cursors.IBeam;
        }
    }
    
    public Cursor EditingPanelCursor
    {
        get
        {
            return Cursors.IBeam;
        }
    }
    
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView; 
        }
        set
        {
            dataGridView = value;
        }
    }
    
    public object EditingControlFormattedValue
    {
        get
        {
            return this.txtFile.Text;
        }
        set
        {
            this.txtFile.Text = value.ToString();
        }
    }
    
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }
    
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged; 
        } 
        set
        { 
            valueChanged = value;
        }
    }
    
    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
    {
        switch (keyData)
        {
            case Keys.Tab: return true;
            case Keys.Home:
            case Keys.End:
            case Keys.Left:
                if (this.txtFile.SelectionLength == this.txtFile.Text.Length)
                    return false;
                else
                    return true;
            case Keys.Right: return true;
            case Keys.Delete: this.txtFile.Text = "";
                return true;
            case Keys.Enter: NotifyChange();
                return false;
            default: return false;
        }
    }
    
    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return this.txtFile.Text;
    }
    
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        if (this.dataGridView.CurrentCell.Value == null)
            this.txtFile.Text = "";
        else
            this.txtFile.Text = this.dataGridView.CurrentCell.Value.ToString(); 
        
        if (selectAll)
            this.txtFile.SelectAll();
        oldValue = this.txtFile.Text; 
    }
    
    public bool RepositionEditingControlOnValueChange { 
        get 
        {
            return false;
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DGVFileEditingControl));
        this.btnBrowse = new System.Windows.Forms.Button();
        this.ofdFile = new System.Windows.Forms.OpenFileDialog();
        this.txtFile = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // btnBrowse
        // 
        this.btnBrowse.AutoEllipsis = true;
        this.btnBrowse.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.btnBrowse.Dock = System.Windows.Forms.DockStyle.Right;
        this.btnBrowse.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
        this.btnBrowse.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
        this.btnBrowse.Image = ((System.Drawing.Image)(resources.GetObject("btnBrowse.Image")));
        this.btnBrowse.Location = new System.Drawing.Point(87, 0);
        this.btnBrowse.Margin = new System.Windows.Forms.Padding(0);
        this.btnBrowse.Name = "btnBrowse";
        this.btnBrowse.Size = new System.Drawing.Size(27, 32);
        this.btnBrowse.TabIndex = 0;
        this.btnBrowse.UseVisualStyleBackColor = true;
        this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
        // 
        // txtFile
        // 
        this.txtFile.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.txtFile.Dock = System.Windows.Forms.DockStyle.Fill;
        this.txtFile.Location = new System.Drawing.Point(0, 0);
        this.txtFile.Multiline = true;
        this.txtFile.Name = "txtFile";
        this.txtFile.Size = new System.Drawing.Size(87, 32);
        this.txtFile.TabIndex = 1;
        // 
        // DGVFileEditingControl
        // 
        this.Controls.Add(this.txtFile);
        this.Controls.Add(this.btnBrowse);
        this.Name = "DGVFileEditingControl";
        this.Size = new System.Drawing.Size(114, 32);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void txtFilePath_TextChanged(object sender, EventArgs e)
    {

    }
}